/*
 * KamusParser.java
 *
 * Created on August 13, 2008, 11:07 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.kamus.iwic.main;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author mamat-dev
 */
public class KamusParser {
   /** Creates a new instance of KamusParser */
    public KamusParser() {
    }
       
    public Vector parserXMlKamus(String xml){
        ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
        Vector vec = new Vector();
        vec.removeAllElements();
        KXmlParser parser = new KXmlParser();       
        int index = 0;
        try {
            parser.setInput(new InputStreamReader(is));
            parser.next();
            parser.require(XmlPullParser.START_TAG, null,"kamus");
            while(parser.nextTag()!=XmlPullParser.END_TAG){
                parser.require(XmlPullParser.START_TAG,null, "data");
                while(parser.nextTag()!=XmlPullParser.END_TAG){                  
                    Kamus k = new Kamus();
                    parser.require(XmlPullParser.START_TAG, null, "id");
                    k.setId(parser.nextText());
                    parser.require(XmlPullParser.END_TAG, null, "id");
                    parser.nextTag();
                    parser.require(XmlPullParser.START_TAG, null, "kata");
                    k.setKata(parser.nextText());
                    parser.require(XmlPullParser.END_TAG, null, "kata");
                    parser.nextTag();
                    parser.require(XmlPullParser.START_TAG, null, "deskripsi");
                    k.setDeskripsi(parser.nextText());
                    parser.require(XmlPullParser.END_TAG, null, "deskripsi");
                    vec.addElement(k);
                    index++;
                }
                parser.require(XmlPullParser.END_TAG, null, "data");
            }
            parser.require(XmlPullParser.END_TAG, null, "kamus");
            return vec;
        } catch (IOException ex) {
            //ex.printStackTrace();
            System.out.println("Excep");
        } catch (XmlPullParserException ex) {
            //ex.printStackTrace();
            System.out.println("Parser");
        }
        return vec;
    }
}
